package com.ictconnect.database.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import com.ictconnect.database.entity.Group;
import com.ictconnect.database.entity.Role;
import com.ictconnect.database.entity.User;

public class HibernateUtil {

	private static final SessionFactory sessionFactory;

	    static {
	        try {
	        	AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration();

	        	annotationConfiguration.addAnnotatedClass(User.class);
	        	annotationConfiguration.addAnnotatedClass(Group.class);
	        	annotationConfiguration.addAnnotatedClass(Role.class);

	            sessionFactory = annotationConfiguration.buildSessionFactory();
	        } catch (Throwable ex) {
	            // Log exception!
	            throw new ExceptionInInitializerError(ex);
	        }
	    }

	    public static Session getSession()
	            throws HibernateException {
	        return sessionFactory.openSession();
	    }
	}